package com.dycx.message.converter;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dycx.message.dto.MessageRecordDTO;
import com.dycx.message.entity.MessageRecordDO;
import com.dycx.message.enums.MessageTypeEnum;
import com.dycx.message.vo.MessageRecordVO;
import lombok.Data;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 消息发送记录(MessageRecord)实体转换类
 *
 * @author wuzongbin
 * @since 2023-11-15 11:02:31
 */


@Data
public class MessageRecordConverter implements Serializable {
    public static MessageRecordDO dtoToEntity(MessageRecordDTO dto) {
        if (dto == null) return null;
        MessageRecordDO entity = BeanUtil.toBean(dto, MessageRecordDO.class);
        return entity;
    }

    public static MessageRecordVO entityToVo(MessageRecordDO entity) {
        if (entity == null) return null;
        MessageRecordVO vo = BeanUtil.toBean(entity, MessageRecordVO.class);
        vo.setMessageTypeName(MessageTypeEnum.converter(vo.getMessageType()));
        return vo;
    }

    public static List<MessageRecordVO> entityToVoList(List<MessageRecordDO> entityList) {
        List<MessageRecordVO> voList = new ArrayList<>();
        for (MessageRecordDO entity : entityList) {
            voList.add(entityToVo(entity));
        }
        return voList;
    }

    public static Page<MessageRecordVO> entityToVoPage(Page<MessageRecordDO> page) {

        List<MessageRecordVO> newRecords = new ArrayList<>();

        List<MessageRecordDO> records = page.getRecords();
        for (MessageRecordDO entity : records) {
            newRecords.add(entityToVo(entity));
        }

        Page<MessageRecordVO> voPage = new Page<>();
        voPage.setRecords(newRecords);
        voPage.setPages(page.getPages());
        voPage.setTotal(page.getTotal());
        voPage.setSize(page.getSize());
        voPage.setCurrent(page.getCurrent());

        return voPage;
    }

}
